package com.jy.adscm.operation.cg.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cg.bean.CgBom;
import com.jy.adscm.operation.cg.bean.CgBomSupplier;
import com.jy.adscm.operation.cg.bean.CgMaterial;
import com.jy.adscm.operation.cg.bean.CgSupplier;
import com.jy.adscm.operation.cg.mapper.mysql.CgBomMapper;
import com.jy.adscm.operation.cg.mapper.mysql.CgBomSupplierMapper;
import com.jy.adscm.operation.cg.mapper.mysql.CgMaterialMapper;
import com.jy.adscm.operation.cg.mapper.mysql.CgSupplierMapper;
import com.jy.adscm.operation.cg.service.CgBomService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.JyDrawingList;
import com.jy.adscm.zltx.bean.JyDrawingType;
import com.jy.adscm.zltx.mapper.mysql.JyDrawingListMapper;
import com.jy.adscm.zltx.mapper.mysql.JyDrawingTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * bom 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-07-07
 */
@Service
public class CgBomServiceImpl extends ServiceImpl<CgBomMapper, CgBom> implements CgBomService {
    @Autowired
    JyDrawingListMapper jyDrawingListMapper;
    @Autowired
    JyUserMapper jyUserMapper;

    @Autowired
    CgSupplierMapper cgSupplierMapper;
    @Autowired
    CgBomSupplierMapper cgBomSupplierMapper;
    @Autowired
    CgMaterialMapper cgMaterialMapper;
    @Autowired
    CgBomMapper cgBomMapper;
    @Autowired
    JyDrawingTypeMapper jyDrawingTypeMapper;

    @Override
    public List<JyDrawingList> selectDrawByParentId(Integer parentId) throws AdscmSystemException {
        List<JyDrawingList> reslut=new ArrayList<>();

        if (parentId==0) {
            Wrapper<JyDrawingList> jyDrawingListWrapper = new EntityWrapper<>();
            jyDrawingListWrapper.eq("parent_id", 0);
            jyDrawingListWrapper.orderBy("rank",false);
            List<JyDrawingList> jyDrawingLists = jyDrawingListMapper.selectList(jyDrawingListWrapper);
            for (JyDrawingList jyDrawingList : jyDrawingLists) {
                Integer drawingType = jyDrawingList.getDrawingType();
                String drawingName = jyDrawingListMapper.selectDrawingTypeName(drawingType);
                jyDrawingList.setDrawingName(drawingName + "：" + jyDrawingList.getDrawingName());
            }
            reslut=jyDrawingLists;

        }else {
            List<JyDrawingList> jyDrawingLists = jyDrawingListMapper.selectByParentId2(Long.valueOf(parentId));
            for (JyDrawingList jyDrawingList : jyDrawingLists) {
                Wrapper<CgBom> cgBomWrapper=new EntityWrapper<>();
                cgBomWrapper.eq("draw_id",jyDrawingList.getId());
                cgBomWrapper.eq("enable",1);
                List<CgBom> cgBoms = super.selectList(cgBomWrapper);
                if (cgBoms!=null && cgBoms.size()>0){
                    jyDrawingList.setHasSame("1");
                    jyDrawingList.setIsUpdate(cgBoms.size()+"");
                }else {
                    jyDrawingList.setHasSame("0");
                    jyDrawingList.setIsUpdate("");
                }
            }

            reslut=jyDrawingLists;
        }

        return reslut;
    }

    @Override
    public List<CgBom> selectByDrawId(Integer drawId) {
        Wrapper<CgBom> wrapper=new EntityWrapper<>();
        wrapper.eq("draw_id",drawId);
        List<CgBom> cgBoms = super.selectList(wrapper);
        for (CgBom cgBom : cgBoms) {
            JyUser jyUser = jyUserMapper.selectById(cgBom.getCreator());
            cgBom.setCreatorName(jyUser.getName());
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getMid(),null)){
                CgMaterial cgMaterial = cgMaterialMapper.selectById(cgBom.getMid());
                cgBom.setCgMaterial(cgMaterial);
                cgBom.setName(cgMaterial.getName());
                cgBom.setCode(cgMaterial.getCode());
                cgBom.setType(cgMaterial.getType());
                cgBom.setNo(cgMaterial.getNo());
            }


        }
        return cgBoms;
    }

    @Override
    public boolean deleteCgBom(Integer id) {
        cgBomMapper.deleteCgBom(id);

        return true;
    }

    @Override
    public List<CgBom> selectByMid(Integer mid) {
        Wrapper<CgBom> wrapper=new EntityWrapper<>();
        wrapper.eq("mid",mid);
        List<CgBom> cgBoms = super.selectList(wrapper);
        for (CgBom cgBom : cgBoms) {
            //机型
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getMachineType(),null)){
                JyDrawingType jyDrawingType = jyDrawingTypeMapper.selectById(cgBom.getMachineType());
                cgBom.setMachineTypeName(jyDrawingType.getName());
            }
            if(AdscmBaseUtils.getInstance().isNotEmpty(cgBom.getDrawId(),null)){
                JyDrawingList jyDrawingList = jyDrawingListMapper.selectById(cgBom.getDrawId());
                cgBom.setDrawIdName(jyDrawingList.getDrawingName());
            }
        }
        return cgBoms;
    }
}
